package org.particlethink.singleflight;

import org.particlethink.job.Result;

import java.util.Date;

public class SingleResult<T> implements Result<T> {

    private T result;
    private final Date creationDate;
    private boolean done = false;
    private Exception exception;

    public SingleResult(T result, Date creationDate,boolean done) {
        this.result = result;
        this.creationDate = creationDate;
        this.done = done;
    }

    public SingleResult(T result, Date creationDate, boolean done, Exception exception) {
        this.result = result;
        this.creationDate = creationDate;
        this.done = done;
        this.exception = exception;
    }

    public SingleResult(Date creationDate) {
        this.creationDate = creationDate;
    }

    public void setResult(T result) {
        this.result = result;
    }

    @Override
    public T getResult() {
        return this.result;
    }

    @Override
    public Date creationTime() {
        return this.creationDate;
    }

    public boolean isDone() {
        return done;
    }

    @Override
    public void done() {
        this.done = true;
    }

    public void setDone(boolean done) {
        this.done = done;
    }

    public Exception getException() {
        return exception;
    }

    public void setException(Exception exception) {
        this.exception = exception;
    }

    public void copy(Result<T> other) {
        this.result = other.getResult();
        this.exception = other.getException();
        this.done = other.isDone();
    }
}
